package com.modern.js.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.modern.js.entity.Contact;
import com.modern.js.entity.Employee;
import com.modern.js.pojo.ContactPojo;
import com.modern.js.service.IContactService;
import com.modern.js.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author PIGS
 * @since 2020-03-28
 */
@RestController
@RequestMapping("/contact")
public class ContactController {
    @Autowired
    private IContactService iContactService;

    @GetMapping("/list")
    public Map selectList(Integer page, Integer limit){
        System.out.println("你好世界。。。。。。。。。");
        Map<String,Object> map = new HashMap<String,Object>();
        Page<ContactPojo> contactPojoPage = new Page<ContactPojo>();
        contactPojoPage.setSize(limit);//设置每页记录数
        contactPojoPage.setCurrent(page);//设置当前页码
        IPage<ContactPojo> iPage = iContactService.selectAll(contactPojoPage);
        if (iPage.getTotal()!=0){
            map.put("msg","查询成功！");
        }else {
            map.put("msg","查询失败！");
        }
        map.put("data",iPage.getRecords());
        map.put("count",iPage.getTotal());
        map.put("code",0);
        return map;
    }

    @DeleteMapping("/del/{contactId}")
    public boolean delContact(@PathVariable Integer contactId){
        Contact contact = new Contact();
        contact.setContactId(contactId);
        contact.setIsDel(0);
        boolean result = iContactService.updateById(contact);
        return result;
    }

    @PostMapping("/add")
    public boolean addContact(Contact contact, HttpSession session){
        Employee employee =(Employee) session.getAttribute("employee");
        Integer empId = employee.getEmpId();
        contact.setEmpId(empId);
        contact.setIsDel(1);
        contact.setCreateTime(StringUtils.getNowTime());
        contact.setUpdateTime(StringUtils.getNowTime());
        boolean result = iContactService.save(contact);
        return result;
    }


    @GetMapping("/selectById")
    public Map selectById(Integer page, Integer limit,HttpSession session){
        Employee employee = (Employee) session.getAttribute("employee");
        Integer empId = employee.getEmpId();
        Map<String,Object> map = new HashMap<String,Object>();
        Page<ContactPojo> contactPojoPage = new Page<ContactPojo>();
        contactPojoPage.setSize(limit);//设置每页记录数
        contactPojoPage.setCurrent(page);//设置当前页码
        IPage<ContactPojo> iPage = iContactService.selectById(contactPojoPage,empId);
        if (iPage.getTotal()!=0){
            map.put("msg","查询成功！");
        }else {
            map.put("msg","查询失败！");
        }
        map.put("data",iPage.getRecords());
        map.put("count",iPage.getTotal());
        map.put("code",0);
        return map;
    }
}
